var ydb = wx.cloud.database()
const _ = ydb.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    roomList: [],
    isNull: false,
    chooseFlag: false,
    chooseContainNum: null,
    containNumFlag: [false, false, false],
    chooseTime: null,
    timeFlag: [false, false, false, false, false, false],
  },

  /* 加载数据 */
  async getList() {
    wx.showLoading({
      title: '载入数据中',
    })
    let res = await ydb.collection("meetingRoom").skip(this.page * this.PAGE).limit(this.PAGE).get({})
    wx.hideLoading()
    let data = res.data;
    if (this.init) { //初始化

      if (data.length > 0) {
        this.setData({
          roomList: data
        })
      } else {
        this.setData({
          isNull: true
        })
      }
    } else { //追加
      let conData = this.data.roomList.concat(data);
      this.setData({
        roomList: conData
      })
    }

  },

  /* 触底时进行加载 */
  async onReachBottom() {
    this.page += 1;
    this.init = false;
    await this.getList()
  },

  /* 筛选框 */
  choosePanel: function (e) {
    this.setData({
      chooseFlag: !this.data.chooseFlag
    })
  },

  /* 图片取消筛选框 */
  cancel: function () {
    this.setData({
      chooseFlag: !this.data.chooseFlag
    })
  },

  /* 选择Item */
  clickItem: function (e) {
    let data = e.currentTarget.dataset;
    if (data.num) {
      let flag = !this.data.containNumFlag[Number(e.currentTarget.dataset.num)]
      this.setData({
        chooseContainNum: e.currentTarget.dataset.num,
        ["containNumFlag[" + Number(e.currentTarget.dataset.num) + "]"]: flag,
      })
    } else {
      let flag = !this.data.timeFlag[Number(e.currentTarget.dataset.timeflag)]
      this.setData({
        chooseTime: e.currentTarget.dataset.time,
        ["timeFlag[" + Number(e.currentTarget.dataset.timeflag) + "]"]: flag,
      })
    }

  },

  /* 重置 */
  reset: function () {
    this.setData({
      chooseContainNum: null,
      chooseTime: null
    })
  },

  /* 确定 */
  async confirm() {
    this.setData({
      chooseFlag: !this.data.chooseFlag,
      roomList: null,
      isNull: false
    })

    wx.showLoading({
      title: '载入数据中',
    })

    var that = this
    //所选择的人数,日期，时间段
    var cNum = that.data.chooseContainNum

    var cTime = that.data.chooseTime
    console.log("我获取的数据")
    console.log("cNum" + cNum)
    console.log("cTime" + cTime)
    //只选人数
    if (cNum != null && cTime == null) {
      ///获取人数符合的队列
      let numCorrectListRes = await that.myNumCorrectF(cNum);
      wx.hideLoading();
      //结果保存在页面数据的resultList中
      that.setData({
        roomList: numCorrectListRes
      })
    }

    //只选时段
    if (cNum == null && cTime != null) {
      let allRes = await ydb.collection('meetingRoom').get({})
      //*符合的结果中检索时间符合的对象*/
      let myFinalRes = that.myTimeCorrectF(allRes.data, cTime);
      console.log(myFinalRes)
      wx.hideLoading();
      that.setData({
        roomList: myFinalRes
      })
    }
    //都选
    if (cNum != null && cTime != null) {
      ///获取人数符合的队列
      let numCorrectListRes = await that.myNumCorrectF(cNum)
      console.log("numCorrectListRes")
      console.log(numCorrectListRes)
      let numCorrectList = numCorrectListRes
      console.log("numCorrectList")
      console.log(numCorrectList)
      //*在人数符合的结果中检索时间符合的对象*/
      let myFinalRes = that.myTimeCorrectF(numCorrectList, cTime)
      console.log("所符合的队列")
      console.log(myFinalRes)
      ////结果保存在页面数据的resultList中
      wx.hideLoading();
      that.setData({
        roomList: myFinalRes
      })
    }

    //都没选
    if (cNum == null && cTime == null) {
      wx.hideLoading()
      wx.showToast({
        title: '未选择筛选条件，展示所有会议室！',
        icon: "none"
      })
      setTimeout(() => {
        this.loadAll()
      }, 1000)
    }


    if (this.data.roomList.length <= 0) {
      this.setData({
        isNull: true
      })
    } else {
      this.setData({
        isNull: false
      })
    }

  },

  /* 选择所有 */
  async loadAll() {
    this.setData({
      roomList: null,
      isNull: false
    })
    wx.showLoading({
      title: '载入数据中',
    })

    await this.getList()
    
  },


  //筛选符合人数的队列
  async myNumCorrectF(cNum) {
    var that = this
    //人数符合结果的队列
    var numCorrectList = null
    var numCorrectRes = null
    //检索小于20人符合时间的队列
    if (parseInt(cNum) == 1) {
      numCorrectRes = await ydb.collection('meetingRoom').where({
        //容纳人数小于20的会议室
        containNum: _.lt(20)
      }).get({})
    } else if (parseInt(cNum) == 2) {
      numCorrectRes = await ydb.collection('meetingRoom').where({
        //20-50
        containNum: _.gte(20).lte(50)
      }).get({})
    } else {
      numCorrectRes = await ydb.collection('meetingRoom').where({
        //大于50
        containNum: _.gt(50)
      }).get({})
    }
    numCorrectList = numCorrectRes.data
    console.log("numCorrectList in F")
    console.log(numCorrectList)
    return numCorrectList
  },

  //筛选当天时间符合的对象
  myCheckTimeF(numCorrectList, cTime, chooseDay) { //参数1：人数符合的结果队列，参数3：日期，今天0，明天1，后天2
    var that = this
    let naResult = []
    let resultList = []
    for (var i = 0; i < numCorrectList.length; i++) {
      /*/检查当天时间正确的/*/
      //获取角标
      var timeListIndex = numCorrectList[i].timeList.length - (3 - chooseDay)
      console.log("numCorrectList")
      console.log(numCorrectList)
      console.log(numCorrectList[i].timeList[timeListIndex].openTime)
      //当日的时间可用
      if ([cTime] in numCorrectList[i].timeList[timeListIndex].openTime) {
        if (numCorrectList[i].timeList[timeListIndex].openTime[cTime].state == 1) {
          //将此项压入结果队列
          resultList.push(numCorrectList[i])
        } else {
          //当日的日期不可用
          naResult.push(numCorrectList[i])
        }
      } else {
        naResult.push(numCorrectList[i])
      }
    }
    return {
      naResult: naResult,
      resultList: resultList
    }
  },

  //筛选三天符合结果的对象
  myTimeCorrectF(numCorrectList, cTime) {
    var that = this
    var resultList = [];
    //检测今天符合的对象
    let myTodayList0 = that.myCheckTimeF(numCorrectList, cTime, 0)
    //将符合条件的结果打入结果队列
    for (var t0 = 0; t0 < myTodayList0.resultList.length; t0++) {
      resultList.push(myTodayList0.resultList[t0])
    }

    //不符合条件的检测明天是否符合条件
    //检测明天的会议室有没有空
    let myTodayList1 = that.myCheckTimeF(myTodayList0.naResult, cTime, 1)
    //将符合条件的结果打入结果队列
    for (var t1 = 0; t1 < myTodayList1.resultList.length; t1++) {
      resultList.push(myTodayList1.resultList[t1])
    }

    //不符合条件的检测后天是否符合条件
    //检测后天的会议室有没有空
    let myTodayList2 = that.myCheckTimeF(myTodayList1.naResult, cTime, 2)
    //将符合条件的结果打入结果队列
    for (var t2 = 0; t2 < myTodayList2.resultList.length; t2++) {
      resultList.push(myTodayList1.resultList[t2])
    }
    return resultList
  },

  /* 会议室详细信息 */
  detailPanel: function (options) {
    wx.navigateTo({
      url: '/pages/meetingDetail/meetingDetail?id=' + options.currentTarget.dataset.id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.page = 0;
    this.PAGE = 6;
    this.init = true;
    this.setData({
      chooseFlag: false,
      isNull: false
    })
    await this.getList();
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad()
  }

})